<?php

namespace app\web\controller;

use app\common\lib\Jssdk;
use \think\Controller;
use think\Env;
use think\Log;

class AppLink extends Controller
{

    protected function _initialize(): void
    {
        parent::_initialize();
    }


    /**
     * 通用 applink 跳转
     * 本地地址：
     * 准了：https://local-beta-wechat.goddessxzns.com/web/app_link/index/?app=zhunle&target=link&link=https%3A%2F%2Fwww.baidu.com%3Fneed_auth%3D0
     * 准有好事：https://local-beta-wechat.goddessxzns.com/web/app_link/index/?app=zyhs&target=link&link=https%3A%2F%2Fwww.baidu.com%3Fneed_auth%3D0
     * 好准：https://local-beta-wechat.goddessxzns.com/web/app_link/index/?app=haozhun&target=link&link=https%3A%2F%2Fwww.baidu.com%3Fneed_auth%3D0
     * 准了咨询师：https://local-beta-wechat.goddessxzns.com/web/app_link/index/?app=zlcr&target=link&link=https%3A%2F%2Fwww.baidu.com%3Fneed_auth%3D0
     * @return mixed
     */
    public function index()
    {
        //app的scheme url
        $app = input('app', '');
        $target = input('target', '');
        $link = input('link', '');

        //页面传递的总数据
        $data = [];
        //异常信息
        $error_msg = '';
        //微信签名信息（不在微信里打开是空的）
        $signPackage = [
            'appId' => '',
            'timestamp' => '',
            'nonceStr' => '',
            'signature' => '',
        ];
        //微信分享数据
        $share_data = [
            'title' => '',
            'introduction' => '',
            'img' => '',
            'url' => '',
        ];
        //是否是debug模式
        $debug = 1;

        //test
        //异常提示
//        $error_msg = 'error';
        //test


        try
        {
            //是否是debug模式
            $env = Env::get('host.name');
            if ($env === 'online')
            {
                $debug = 0;
            }

            //处理App的名字 和 下载地址
            $app_name = '';//App的名字
            $download_urls = ['ios' => '', 'android' => '',];//App的下载地址
            $color_bg = '#FAFFFF';//页面的背景色
            $color_btn = '#00D8D3';//按钮的颜色
            $app_id_wechat = '';//微信开放平台里面绑定的App的id

            switch ($app)
            {
                case 'zhunle':
                    $app_name = '准了App';
                    $download_urls['ios'] = 'https://apps.apple.com/cn/app/%E5%87%86%E4%BA%86-%E6%98%9F%E5%BA%A7%E6%83%85%E6%84%9F%E8%A7%A3%E6%9E%90/id1356471277';
                    $download_urls['android'] = 'https://sj.qq.com/appdetail/com.constellation.goddess';
                    $app_id_wechat = 'wx1c9d52b9f3ee2a2b';
                    //分享介绍
                    $share_data['introduction'] = '准了带你了解更多星座奥秘';
                    break;
                case 'zyhs':
                    $app_name = '准有好事App';
                    $download_urls['ios'] = 'https://apps.apple.com/cn/app/%E5%87%86%E6%9C%89%E5%A5%BD%E4%BA%8B-%E7%9C%9F%E4%BA%BA%E6%98%9F%E5%BA%A7%E6%83%85%E6%84%9F%E8%BF%9E%E7%BA%BF/id6468976127';
                    $download_urls['android'] = 'https://sj.qq.com/appdetail/com.zhunle.rtc';
                    $app_id_wechat = 'wx4c5a0384596169af';
                    //分享介绍
                    $share_data['introduction'] = '1对1连线咨询高质量服务';
                    break;
                case 'haozhun':
                    $app_name = '好准App';
                    $download_urls['ios'] = 'https://apps.apple.com/cn/app/%E5%A5%BD%E5%87%86-ai%E5%92%A8%E8%AF%A2%E5%B8%88%E5%9C%A8%E7%BA%BF%E8%A7%A3%E6%83%91/id6449962914';
                    $download_urls['android'] = 'https://sj.qq.com/appdetail/com.haozhun.gpt';
                    $color_bg = '#F6FAFF';
                    $color_btn = '#5288FF';
                    //分享介绍
                    $share_data['introduction'] = '首个AI占星顾问有问必答';
                    break;
                case 'zlcr':
                    $app_name = '准了咨询师App';
                    $download_urls['ios'] = 'https://apps.apple.com/cn/app/%E5%87%86%E4%BA%86%E5%92%A8%E8%AF%A2%E5%B8%88/id6451134962';
                    $download_urls['android'] = 'https://www.goddessxzns.com/zhunle_consulter';
                    //分享介绍
                    $share_data['introduction'] = '客户为大服务至上';
                    break;
                default:
                    $error_msg = '参数异常！';
                    break;
            }

            //分享数据
            $share_data['title'] = $app_name;
            $share_data['img'] = Env::get('host.domain') . '/static/app_link/wechat_share_' . $app . '.png';
            $share_data['url'] = Env::get('host.domain') . '/app_link?app=' . $app . '&target=' . $target . '&link=' . $link;

            //判断是否是微信浏览器
            //todo 只有准了有 准了官方服务号，所以只有准了能，网页跳转移动应用（等其他的公众号搞好后再搞）
            $user_agent = $_SERVER['HTTP_USER_AGENT'];
            if (strpos($user_agent, 'MicroMessenger') !== false)
            {
                // 处理微信浏览器逻辑
                if ($app === 'zhunle')
                {
                    $signPackage = (new Jssdk())->getSignPackage();
                } else if ($app === 'zyhs')
                {
                    $signPackage = (new Jssdk())->getSignPackage('', 'wx393a0263a86fd6f7', '7774fc38a2b8e65da2bd155cf7c86177');
                }
            }


            $data = [
                'app' => $app,
                'target' => $target,
                'link' => urlencode($link),
                'app_name' => $app_name,
                'download_urls' => $download_urls,
                'color_bg' => $color_bg,
                'color_btn' => $color_btn,
                'app_id_wechat' => $app_id_wechat,
            ];
        }
        catch (\Exception $e)
        {
            $error_msg = '无效请求或参数不存在';
            Log::write('通用 applink 跳转：' . $e->getMessage() . '，失败行数：' . $e->getLine(), 'error');
        }

        return $this->fetch('app_link', [
            'data' => $data,
            'error_msg' => $error_msg,
            'signPackage' => $signPackage,
            'share_data' => $share_data,
            'debug' => $debug,
        ]);
    }

}
